package com.raccoonagents.tools.cascet2d.model;

import java.util.LinkedList;
import java.util.Queue;

import org.lwjgl.LWJGLException;

import com.raccoonagents.tools.cascet2d.model.exceptions.FontBuildException;
import com.raccoonagents.tools.cascet2d.model.stage.Stage;
import com.raccoonagents.tools.cascet2d.util.GraphicUtil;
/**
 * 
 * @author Kari Kuvaja
 * The main application class is the master class to be extented by applications.
 * 
 * 
 */
public abstract class MainApp{
	/** list of stages to be run */
	private final static Queue<Stage> stagesToRun = new LinkedList<Stage>();
	/** current stage that is being runned. */
	private static Stage currentStage;
	/**
	 * 
	 * @return the currently running stage
	 */
	public final static Stage getCurrentStage() {
		return currentStage;
	}

	/**
	 * The gameLoop method for stages is the root of logic for a stage.
	 */
	public abstract void gameLoop();
	/**
	 * 
	 * @param width the desired width for the window
	 * @param height the desired height for the window
	 * @param title the title shown
	 * @throws LWJGLException on LWJGL initialization exeption.
	 * @throws FontBuildException if failed to initialize Courier New system font.
	 */
	public MainApp(final int width, final int height, final String title, final boolean fullScreen) throws LWJGLException, FontBuildException{
		GraphicUtil.doGameWindow(width, height, title,"Courier new", fullScreen);
		GraphicUtil.initGL();
	}
	
	/**
	 * @param width
	 * @param height
	 * @param title
	 * @param fullScreen
	 * @param font name of font to create from systemfonts (currently other fonts than courier new aren't well supported)
	 * @throws LWJGLException
	 * @throws FontBuildException
	 */
	public MainApp(final int width, final int height, final String title, final boolean fullScreen, final String font) throws LWJGLException, FontBuildException{
		GraphicUtil.doGameWindow(width, height, title,font,fullScreen);
		GraphicUtil.initGL();
	}
	
	/**
	 * 
	 * gets next stage, sets it as current one and pops it from the queue.
	 * 
	 */
	 public static final Stage getNextStage(){
		currentStage = stagesToRun.remove();
		return currentStage;
		
	}
	/**
	 * adds new stage to the last position of the queue.
	 * @param stage
	 */
	public static final void pushStage(final Stage stage){
		stagesToRun.add(stage);
	}
	/**
	 * @return true, if there are any more stages to run.
	 */
	 public final static boolean hasNextStage(){
		return !stagesToRun.isEmpty();
	}
	
}
